import { ref } from 'vue'
import { getOrganizeList as getOrganizeListAPI } from '@/api/organize'

/**
 * 组织列表服务
 */
export default function () {
  const getOrganizeListLoading = ref(false)
  const organizeList = ref<Array<OrganizeAPIResponse.OrganizeListListItem>>([])

  const getOrganizeList = async () => {
    try {
      getOrganizeListLoading.value = true
      const res = await getOrganizeListAPI()
      getOrganizeListLoading.value = false
      if (res.data.code === 20000) {
        organizeList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (e) {
      getOrganizeListLoading.value = false
      return '获取所有组织失败，请稍后重试'
    }
  }

  return {
    organizeList,
    getOrganizeListLoading,
    getOrganizeList,
  }
}
